package com.carmaintain.servlets.customer;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carmaintain.common.Constants;
import com.carmaintain.common.Utils;
import com.carmaintain.db.CustomerLib;
import com.carmaintain.db.UserLib;
import com.carmaintain.models.ResultObject;

/**
 * Servlet implementation class CommentPlace
 */
public class CommentPlace extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentPlace() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("请使用POST方式调用");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResultObject result = new ResultObject();
		String customerId = "";
		String placeId = "";
		String generalSatisfaction = "";
		String serviceSatisfaction = "";
		String comment = "";
		
		try {
			request.setCharacterEncoding("UTF-8");
			if (!Utils.checkSign(request)) {
				result.setCode(Constants.CODE_SIGN_ERROR);
				result.setMessage(Constants.MESSAGE_WRONG_SIGN);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			
			customerId = UserLib.checkUserLogin(request.getParameter("customer_id"), request.getParameter("usercode"), Constants.USER_TYPE_CUSTOMER);
			if (customerId == null) {
				throw new Exception();
			}
			
			if(Utils.empty(request.getParameter("place_id")) || Utils.empty(request.getParameter("general_satisfaction")) 
					|| Utils.empty(request.getParameter("service_satisfaction"))){
				result.setCode(Constants.CODE_MISS_PARAMATER);
				result.setMessage(Constants.MESSAGE_MISS_PARAMATER);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}else{
				placeId = request.getParameter("place_id").trim();
				generalSatisfaction = request.getParameter("general_satisfaction").trim();
				serviceSatisfaction = request.getParameter("service_satisfaction").trim();
			}
			
			if (!Utils.empty(request.getParameter("comment"))) {
				comment = request.getParameter("comment").trim();
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_WRONG_PARAMATER);
			result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
			Utils.addTestMessage(result, e);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(Utils.genJsonString(result));
			return;
		}
		
		try {
			boolean ret = CustomerLib.commentPlace(customerId, placeId, generalSatisfaction, serviceSatisfaction, comment);
			if (!ret) {
				result.setCode(Constants.CODE_EXCEPTION);
				result.setMessage(Constants.MESSAGE_EXCETION);
			} else {
				result.setCode(Constants.CODE_SUCCESS);
				result.setMessage(Constants.MESSAGE_SUCCESS);
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_EXCEPTION);
			result.setMessage(Constants.MESSAGE_EXCETION);
			Utils.addTestMessage(result, e);
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(Utils.genJsonString(result));
	}

}
